package com.ssyc.student.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.student.R;
import com.ssyc.student.bean.TreasureChestInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 学生端百宝箱套装列表适配器
 * Created by Administrator on 2018/4/12 0012.
 */

public class StTreasuerBackgroundAdapter extends BaseQuickAdapter<TreasureChestInfo.DataBackgroundBean, BaseViewHolder> {
    private Context context;

    //屏幕宽度
    private int screenWidth;

    //ImageView的布局参数
    private LinearLayout.LayoutParams lvLp;
    //使用按钮打的布局参数
    private LinearLayout.LayoutParams tvLp;

    public List<TreasureChestInfo.DataBackgroundBean> data;

    public StTreasuerBackgroundAdapter(Context context, int layoutResId, @Nullable List<TreasureChestInfo.DataBackgroundBean> data) {
        super(layoutResId, data);
        this.context = context;
//        screenWidth = ScreenUtils.getScreenWidth(context);
//        //计算条目的宽度
//        int itemSize = (screenWidth - 16 * 2 - 16 - 8 * 4) / 2 - 16;
//        lvLp = new LinearLayout.LayoutParams(itemSize, itemSize);
//        tvLp = new LinearLayout.LayoutParams(itemSize, UiUtils.dp2px(36));
//        tvLp.topMargin = UiUtils.dp2px(6);
        this.data = data;

    }

    @Override
    protected void convert(BaseViewHolder helper, TreasureChestInfo.DataBackgroundBean item) {

        //条目
        RelativeLayout ll_item = helper.getView(R.id.ll_item);

        //图片
        ImageView lv_img = helper.getView(R.id.lv_img);

        //名称
        TextView tv_name = helper.getView(R.id.tv_name);

        tv_name.setText(item.getName());

        Glide.with(context).load(item.getUrl2()).into(lv_img);

        //选中状态
        ImageView iv_checked = helper.getView(R.id.iv_checked);

        if (item.ischeck) {
            ll_item.setBackground(context.getResources().getDrawable(
                    R.drawable.student_fashionable_itembg_sel));
            iv_checked.setVisibility(View.VISIBLE);
        } else {
            ll_item.setBackground(context.getResources().getDrawable(R.drawable.student_fashionable_itembg_normal));
            iv_checked.setVisibility(View.INVISIBLE);
        }


        //item的点击事件
        ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.ischeck) {
                    ll_item.setBackground(context.getResources().getDrawable(R.drawable.student_fashionable_itembg_normal));
                    iv_checked.setVisibility(View.INVISIBLE);

                    for(int i = 0 ; i < data.size() ; i++) {
                        data.get(i).ischeck = false;
                    }
                    //通过EventBus更改背景
                    EventBus.getDefault().post(new BusInfo("-1",4,item.getGoods_id()+""));
                } else {
                    ll_item.setBackground(context.getResources().getDrawable(
                            R.drawable.student_fashionable_itembg_sel));
                    iv_checked.setVisibility(View.VISIBLE);

                    for(int i = 0 ; i < data.size() ; i++) {
                        data.get(i).ischeck = false;
                    }
                    item.ischeck = true;

                    //通过EventBus更改背景
                    EventBus.getDefault().post(new BusInfo(item.getUrl(),item.getGoods_type(),item.getGoods_id()+""));
                }

                notifyDataSetChanged();
            }
        });
    }
}
